﻿#!/bin/bash
#build the source to get lib
#set -x
MAKELWIP2()
{
    rm -rf $PWD/out/$cur_platform/obj/net/lwip
    rm -rf $PWD/out/$cur_platform/lib/liblwip.a

    lwip1="LOSCFG_NET_LWIP_SACK_1_4=y"
    lwip_ver=$(grep -rn $lwip1 .config)
    if [[ ${lwip_ver} =~ ${lwip1} ]]
    then
        sed -i "s/LOSCFG_NET_LWIP_SACK_1_4=y/\# LOSCFG_NET_LWIP_SACK_1_4 is not set/g" $PWD/.config
        sed -i "s/\# LOSCFG_NET_LWIP_SACK_2_0 is not set/LOSCFG_NET_LWIP_SACK_2_0=y/g" $PWD/.config
        echo "will make lwip 2.0"
    else
        sed -i "s/LOSCFG_NET_LWIP_SACK_2_0=y/\# LOSCFG_NET_LWIP_SACK_2_0 is not set/g" $PWD/.config
        sed -i "s/\# LOSCFG_NET_LWIP_SACK_1_4 is not set/LOSCFG_NET_LWIP_SACK_1_4=y/g" $PWD/.config
        echo "will make lwip 1.4"
    fi

    make lib -j
}
MAKE()
{
    make lib -j
    if [ -e $PWD/out/$cur_platform/lib ]
    then
        echo "lib is exist"
    else
        echo "make error"
        exit 1
    fi
}
MAKE_PLATFORM()
{
    if [ $cur_platform == "hi3516a" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3516cv300" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3516ev200" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3518ev200" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3556av100/cortex-a53_aarch32" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3559av100/cortex-a53_aarch64" ]
    then
        MAKE
    fi

    if [ $cur_platform == "hi3559av100/cortex-m7" ]
    then
        MAKE
    fi

    if [ $cur_platform == "hi3559av100es/cortex-a53_aarch32" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3559av100es/cortex-a53_aarch64" ]
    then
        MAKE
    fi

    if [ $cur_platform == "hi3559av100es/cortex-m7" ]
    then
        MAKE
    fi

    if [ $cur_platform == "hi3559/cortex-a7" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3559/cortex-a17" ]
    then
        MAKE
        MAKELWIP2
    fi

    if [ $cur_platform == "hi3731" ]
    then
        MAKE
        MAKELWIP2
    fi
}

LITEOSTOPDIR=`pwd`
cd ${LITEOSTOPDIR}

src_array=(
kernel/extended/scatter
kernel/extended/dynload
kernel/extended/runstop
kernel/extended/cppsupport
kernel/extended/cpup
kernel/extended/tickless
drivers/base
drivers/cpufreq
drivers/devfreq
drivers/usb
fs/fat/virpart
fs/ramfs
fs/romfs
fs/yaffs2
fs/vfs/bcache
fs/vfs/multi_partition
lib/libsec
platform/cpu/arm/arm-a/cortex-a7
platform/cpu/arm/arm-a/arm926
platform/cpu/arm/arm-a/cortex-a53_aarch32
platform/cpu/arm/arm-a/cortex-a53_aarch64
)

lib_array=(
libscatter.a
libdynload.a
librunstop.a
libcppsupport.a
libcpup.a
libtickless.a
libdriverbase.a
libcpufreq.a
libdevfreq.a
libusb_device.a
libvirpart.a
libramfs.a
libromfs.a
libyaffs2.a
libbcache.a
libmulti_partition.a
libsec.a
libcortex-a7.a
libarm926.a
libcortex-a53_aarch32.a
libcortex-a53_aarch64.a
)


if [ $# == 1 ]
then
support_platform=(
$1
)
else
support_platform=(
hi3516cv300
hi3516ev200
hi3556av100/cortex-a53_aarch32
hi3559av100/cortex-a53_aarch64
)
fi

support_cpu=(
arm926
cortex-a7
cortex-a53_aarch32
cortex-a53_aarch64
)
######## if no input parameter exit shell script #########
if [ $# != 0 ] && [ $# != 1 ]
then
    echo "######## please do not input parameters or only input one parameter such as hi3516a ########"
    exit
fi

# Before starting the code compilation, firstly modify the compile option(ENABLE_LWIP_SACKLIB) of Lwip Makefile from n to m #
sed -i "s/"ENABLE_LWIP_SACKLIB=n"/"ENABLE_LWIP_SACKLIB=m"/" net/lwip_sack/Makefile
sed -i "s/"ENABLE_LWIP_SACKLIB=n"/"ENABLE_LWIP_SACKLIB=m"/" net/lwip-2.0/Makefile
#. env_setup.sh hi3516a
echo "##########step1:build over!!! start to copy libs###########"
for cur_platform in ${support_platform[@]}
do
    echo "platform is : ${cur_platform}"

    chmod 666 ./.config
    chmod 777 ./tools/build/config/debug -R

    if [ ${cur_platform} == "hi3516a" ] || [ ${cur_platform} == "hi3559/cortex-a7" ] || [ ${cur_platform} == "hi3516ev200" ]
    then
        cur_cpu="cortex-a7"
    elif [ ${cur_platform} == "hi3518ev200" ] || [ ${cur_platform} == "hi3731" ] || [ ${cur_platform} == "hi3516cv300" ]
    then
        cur_cpu="arm926"
    elif [ ${cur_platform} == "hi3559/cortex-a17" ]
    then
        cur_cpu="cortex-a17"
    elif [ ${cur_platform} == "hi3556av100/cortex-a53_aarch32" ] || [ ${cur_platform} == "hi3559av100es/cortex-a53_aarch32" ]
    then
        cur_cpu="cortex-a53_aarch32"
    elif [ ${cur_platform} == "hi3559av100es/cortex-a53_aarch64" ] || [ ${cur_platform} == "hi3559av100/cortex-a53_aarch64" ]
    then
        cur_cpu="cortex-a53_aarch64"
    elif [ ${cur_platform} == "hi3559av100/cortex-m7" ] || [ ${cur_platform} == "hi3559av100es/cortex-m7" ]
    then
        cur_cpu="cortex-m7"
    fi

    if [ ${cur_platform} == "hi3559/cortex-a7" ]
    then
        config_file="hi3559_cortex-a7.config"
    elif [ ${cur_platform} == "hi3559/cortex-a17" ]
    then
        config_file="hi3559_cortex-a17.config"
    elif [ ${cur_platform} == "hi3556av100/cortex-a53_aarch32" ]
    then
        config_file="hi3556av100_a53_32bit.config"
    elif [ ${cur_platform} == "hi3559av100/cortex-a53_aarch64" ]
    then
        config_file="hi3559av100_a53_64bit.config"
    elif [ ${cur_platform} == "hi3559av100/cortex-m7" ]
    then
        config_file="hi3559av100_m7.config"
    elif [ ${cur_platform} == "hi3559av100es/cortex-a53_aarch32" ]
    then
        config_file="hi3559av100es_a53_32bit.config"
    elif [ ${cur_platform} == "hi3559av100es/cortex-a53_aarch64" ]
    then
        config_file="hi3559av100es_a53_64bit.config"
    elif [ ${cur_platform} == "hi3559av100es/cortex-m7" ]
    then
        config_file="hi3559av100es_m7.config"
    else
        config_file="${cur_platform}.config"
    fi
    cp $LITEOSTOPDIR/tools/build/config/debug/$config_file ./.config
    rm -fr $LITEOSTOPDIR/platform/bsp/board/${cur_platform}/include/menuconfig.h
    make clean
    MAKE_PLATFORM
    git checkout $LITEOSTOPDIR/platform/bsp/board/${cur_platform}/include/menuconfig.h

    ########### creat dir for platforms ##########
    for dir in ${src_array[@]}
    do
        if [ ${dir} == "platform/cpu/arm/arm-a/cortex-a7" ]
        then
            if [ ${cur_platform} != "hi3516a" ] && [ ${cur_platform} != "hi3559/cortex-a7" ] && [ ${cur_platform} != "hi3516ev200" ]
            then
                continue
            fi
        fi
        if [ ${dir} == "platform/cpu/arm/arm-a/arm926" ]
        then
            if [ ${cur_platform} != "hi3518ev200" ] && [ ${cur_platform} != "hi3731" ] && [ ${cur_platform} != "hi3516cv300" ]
            then
                continue
            fi
        fi
        if [ ${dir} == "platform/cpu/arm/arm-a/cortex-a17" ]
        then
            if [ ${cur_platform} != "hi3559/cortex-a17" ]
            then
                continue
            fi
        fi
        if [ ${dir} == "platform/cpu/arm/arm-a/cortex-a53_aarch32" ]
        then
            if [ ${cur_platform} != "hi3556av100/cortex-a53_aarch32" ] && [ ${cur_platform} != "hi3559av100es/cortex-a53_aarch32" ]
            then
                continue
            fi
        fi
        if [ ${dir} == "platform/cpu/arm/arm-a/cortex-a53_aarch64" ]
        then
            if [ ${cur_platform} != "hi3559av100es/cortex-a53_aarch64" ] && [ ${cur_platform} != "hi3559av100/cortex-a53_aarch64" ]
            then
                continue
            fi
        fi
        if [ ${dir} == "platform/cpu/arm/arm-m/cortex-m7" ]
        then
            if [ ${cur_platform} != "hi3559av100/cortex-m7" ] && [ ${cur_platform} != "hi3559av100es/cortex-m7" ]
            then
                continue
            fi
        fi

        mkdir -p ${dir}/$cur_cpu
    done

    ####### copy the lib to the direct ########
    for index in `seq 0 20`
    do
    if [ ${lib_array[index]} = "libcortex-a7.a" ]
    then
        if [ ${cur_platform} != "hi3516a" ] && [ ${cur_platform} != "hi3559/cortex-a7" ] && [ ${cur_platform} != "hi3516ev200" ]
        then
            continue
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    fi
    if [ ${lib_array[index]} = "libcortex-a17.a" ]
    then
        if [ ${cur_platform} != "hi3559/cortex-a17" ]
        then
            continue
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    fi
    if [ ${lib_array[index]} == "libarm926.a" ]
    then
        if [ ${cur_platform} != "hi3518ev200" ] && [ ${cur_platform} != "hi3731" ] && [ ${cur_platform} != "hi3516cv300" ]
        then
            continue
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    fi
    if [ ${lib_array[index]} == "libcortex-a53_aarch32.a" ]
    then
        if [ ${cur_platform} != "hi3556av100/cortex-a53_aarch32" ] && [ ${cur_platform} != "hi3559av100es/cortex-a53_aarch32" ]
        then
            continue
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    fi
    if [ ${lib_array[index]} == "libcortex-a53_aarch64.a" ]
    then
        if [ ${cur_platform} != "hi3559av100/cortex-a53_aarch64" ] && [ ${cur_platform} != "hi3559av100es/cortex-a53_aarch64" ]
        then
            continue
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    fi
    if [ ${lib_array[index]} == "libcortex-m7.a" ]
    then
        if [ ${cur_platform} != "hi3559v100/cortex-m7" ] && [ ${cur_platform} != "hi3559av100es/cortex-m7" ]
        then
            continue
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    fi

    if [ $# == 1 ] && [ -e out/${cur_platform}/lib/${lib_array[index]} ]
    then
        cp  out/${cur_platform}/lib/${lib_array[index]} ${src_array[index]}/$cur_cpu/
        if [ ${lib_array[index]} == "libusb_device.a" ]
        then
            mv -f ${src_array[index]}/gadget/libmk ${src_array[index]}/gadget/Makefile
        else
            mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
        fi
    else
        cp out/${cur_platform}/lib/${lib_array[index]} ${src_array[index]}/$cur_cpu/
        if [ ${lib_array[index]} == "libusb_device.a" ] && [ -e ${src_array[index]}/gadget/libmk ] || [ -e ${src_array[index]}/libmk ]
        then
            if [ -e ${src_array[index]}/${support_cpu[0]} ] && [ -e ${src_array[index]}/${support_cpu[1]} ] && [ -e ${src_array[index]}/${support_cpu[2]} ] && [ -e ${src_array[index]}/${support_cpu[3]} ]
            then
                if [ ${lib_array[index]} == "libusb_device.a" ]
                then
                    mv -f ${src_array[index]}/gadget/libmk ${src_array[index]}/gadget/Makefile
                else
                    mv -f ${src_array[index]}/libmk ${src_array[index]}/Makefile
                fi
            fi
        fi
    fi
    done

    make clean
done

echo "##########step2:copy over!!! delete the source###########"
for dir in ${src_array[@]}
do
    ##usb文件单独删除
    if [ ${dir} == "drivers/usb" ]
    then
        continue
    fi
    find ${dir} -name "*.[csS]" | xargs rm -rf
done
##delete some file of usb##
find $LITEOSTOPDIR/drivers/usb/gadget -name "*.c" | xargs rm -rf
find $LITEOSTOPDIR/drivers/usb/controller/usb_device -name "*.c" | xargs rm -rf

# After the code compilation, modify the compile option(ENABLE_LWIP_SACKLIB) of Lwip Makefile from m to y #
sed -i "s/"ENABLE_LWIP_SACKLIB=m"/"ENABLE_LWIP_SACKLIB=y"/" net/lwip_sack/Makefile
sed -i "s/"ENABLE_LWIP_SACKLIB=m"/"ENABLE_LWIP_SACKLIB=y"/" net/lwip-2.0/Makefile

#delete some files of Lwip#
rm -rf net/lwip_sack/src/core/tcp.c
rm -rf net/lwip_sack/src/core/tcp_in.c
rm -rf net/lwip_sack/src/core/tcp_out.c
rm -rf net/lwip_sack/src/core/tcp_sack.c
rm -rf net/lwip_sack/src/core/tcp_ws.c

rm -rf net/lwip-2.0/src/core/tcp.c
rm -rf net/lwip-2.0/src/core/tcp_in.c
rm -rf net/lwip-2.0/src/core/tcp_out.c
rm -rf net/lwip-2.0/src/core/tcp_sack.c

rm -rf net/wpa_supplicant

#rm -rf test/
rm -rf vendor/
rm -rf 3rdParty/
rm -rf components/
rm -rf platform/bsp/board/hi3731/
rm -rf platform/cpu/arm/arm-m/
rm -rf platform/cpu/arm/arm-a/cortex-a17/
rm -rf platform/cpu/arm/arm-a/common/

vendor="vendor\/Kconfig"
#Test="test\/Kconfig"
Party="3rdParty\/Kconfig"

sed -i '/'"$vendor"'/d' tools/menuconfig/extra/Configs/Config.in
#sed -i '/'"$Test"'/d' tools/menuconfig/extra/Configs/Config.in
sed -i '/'"$Party"'/d' tools/menuconfig/extra/Configs/Config.in

cd $LITEOSTOPDIR
find  ./ -name ".git" |xargs rm -rf
cd -
